package com.quasar.hpatient.module.comm_guide;

import android.graphics.Color;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.TextView;

import com.quasar.hpatient.R;
import com.quasar.hpatient.module.comm_login.LoginActivity;

import lib.quasar.base.bar.BarManger;
import lib.quasar.base.frame.BaseActivity;
import lib.quasar.db.manager.DBManager;
import lib.quasar.db.table.User;
import lib.quasar.widget.indicator.IndicatorView;

/**
 * description: 忘记密码
 * created by kalu on 2018/3/22 16:57
 */
public class GuideActivity extends BaseActivity<GuidePresenter> implements GuideView {

    @Override
    public int initView() {
        return R.layout.activity_comm_guide;
    }

    @Override
    public void initDataLocal() {

        findViewById(R.id.activity_comm_guide_next).setOnClickListener(view -> {

            User model = DBManager.getInstance().syncGetUserModel();
            model.setGuide(false);
            DBManager.getInstance().updataUserModel(model);

            goNext(LoginActivity.class);
            onBackPressed();
        });

        GuidePresenter presenter = getPresenter();
        if (null == presenter) return;
        presenter.initAdapter(this);
    }

    @Override
    public void initDataNet() {
    }

    @Override
    public void setImageAdapter(PagerAdapter adapter) {

        ViewPager pager = findViewById(R.id.activity_comm_guide_pager);
        if (null == pager) return;

        IndicatorView indicator = findViewById(R.id.activity_comm_guide_indicator);
        if (null == indicator) return;

        indicator.attach2ViewPage(pager, adapter.getCount());
        pager.setOffscreenPageLimit(4);
        pager.setAdapter(adapter);
        pager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
            }

            @Override
            public void onPageSelected(int position) {

                String color = position == 0 ? "#13ad67"
                        : position == 1 ? "#ff9e21"
                        : position == 2 ? "#1296db"
                        : "#ea5757";
                BarManger.setColor(GuideActivity.this, Color.parseColor(color));

                TextView next = findViewById(R.id.activity_comm_guide_next);
                if (null != next) {
                    next.setVisibility(position == 3 ? View.VISIBLE : View.GONE);
                }
                IndicatorView indicator = findViewById(R.id.activity_comm_guide_indicator);
                if (null != indicator) {
                    indicator.setVisibility(position == 3 ? View.GONE : View.VISIBLE);
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {
            }
        });
    }
}